package com.web.util;

import java.util.Map;

import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;

public class WebUtils {

	public static String formattedTimeFromMinutes(Integer minutes){
		String ret="  kürzer als";
		
		return ret+ formattedTimeFromMinutes1(minutes);
	}
	
	public static String formattedTimeFromMinutes1(Integer minutes){
		String ret="  ";
		int hours=0;
		
		if(minutes!=null){
			hours=minutes/60;
			minutes=minutes%60;
			
			if(hours>0){
				ret+=hours+(hours == 1 ? " Stunde " : " Stunden ");
			}
			
			if(minutes > 0)
				ret+=minutes+(minutes == 1 ? " Minute " : " Minuten ");
		}
		
		return ret;
	}
	
	
	public static String redirect(String page){
		if(page!=null){
			if("".equals(page))
				return "";   //same page
			else
				return page+"?faces-redirect=true";  //navigate to other page
		}
		else{
			throw new RuntimeException ("Keine Form gefunden!!!");
		}
	}
	
	
	public static Object getSessionObject(String objName) {
	    FacesContext ctx = FacesContext.getCurrentInstance();
	    ExternalContext extCtx = ctx.getExternalContext();
	    Map<String, Object> sessionMap = extCtx.getSessionMap();
	    return sessionMap.get(objName);
	}
	
	public static Object setSessionObject(String objName,Object o) {
	    FacesContext ctx = FacesContext.getCurrentInstance();
	    ExternalContext extCtx = ctx.getExternalContext();
	    Map<String, Object> sessionMap = extCtx.getSessionMap();
	    return sessionMap.put(objName, o);
	}
	
	public static Object deleteSessionObject(String objName) {
	    FacesContext ctx = FacesContext.getCurrentInstance();
	    ExternalContext extCtx = ctx.getExternalContext();
	    Map<String, Object> sessionMap = extCtx.getSessionMap();
	    return sessionMap.remove(objName);
	}
}
